package com.hw.one.dam2.dao;

import com.hw.one.dam2.param.SiteAirParam;
import com.hw.one.dam2.po.SiteAirConcentrationPO;
import com.hw.one.dam2.po.SiteAirQualityPO;
import com.hw.one.dam2.util.AQIComparatorMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * Copyright 2017 汉威.智慧环保事业部.
 * Created by 李亚坤 on 2017/06/28 09:53.
 */
@Repository(value = "siteAirDao")
public class SiteAirDao implements ISiteAirDao {

    @Autowired
    private MongoTemplate mongoTemplateStandardSiteData;

    private static String SITE_AIR_MIN = "T_DATA_AIR_MIN";
    private static String SITE_AIR_HOUR = "T_DATA_AIR_HOUR";
    private static String SITE_AIR_DAY = "T_DATA_AIR_DAY";
    private static String SITE_AIR_WEEK = "T_DATA_AIR_WEEK";
    private static String SITE_AIR_MONTH = "T_DATA_AIR_MONTH";
    private static String SITE_AIR_YEAR = "T_DATA_AIR_YEAR";

    @Override
    public List<SiteAirQualityPO> findSiteAirMinDataBySiteCodesAndTime(SiteAirParam smallAirParam) {
        // 拼凑查询条件
        Criteria criteria = Criteria.where("SITECODE").in(smallAirParam.getSiteCodes());
        criteria.and("MONITORTIME").gte(smallAirParam.getStartTime()).lte(smallAirParam.getEndTime());
        // 创建查询对象,增加排序
        Query query = new Query(criteria).with(new Sort(Sort.Direction.DESC, "MONITORTIME").and(new Sort(Sort.Direction.DESC, "COMPOSITEVALUE")));

        List<SiteAirQualityPO> resultList = mongoTemplateStandardSiteData.find(query,
                SiteAirQualityPO.class, SITE_AIR_MIN + "-" + smallAirParam.getEndTime().substring(0, 4));

        return resultList;
    }

    @Override
    public List<SiteAirQualityPO> findSiteAirHourDataBySiteCodesAndTime(SiteAirParam smallAirParam) {
        // 拼凑查询条件
        Criteria criteria = Criteria.where("SITECODE").in(smallAirParam.getSiteCodes());
        criteria.and("MONITORTIME").gte(smallAirParam.getStartTime()).lte(smallAirParam.getEndTime());
        // 创建查询对象,增加排序
        Query query = new Query(criteria).with(new Sort(Sort.Direction.DESC, "MONITORTIME").and(new Sort(Sort.Direction.DESC, "COMPOSITEVALUE")));

        List<SiteAirQualityPO> resultList = mongoTemplateStandardSiteData.find(query,
                SiteAirQualityPO.class, SITE_AIR_HOUR + "-" + smallAirParam.getEndTime().substring(0, 4));

        return resultList;
    }

    @Override
    public List<SiteAirQualityPO> findSiteAirDayDataBySiteCodesAndTime(SiteAirParam smallAirParam) {
        // 拼凑查询条件
        Criteria criteria = Criteria.where("SITECODE").in(smallAirParam.getSiteCodes());
        criteria.and("MONITORTIME").gte(smallAirParam.getStartTime()).lte(smallAirParam.getEndTime());
        // 创建查询对象,增加排序
        Query query = new Query(criteria).with(new Sort(Sort.Direction.DESC, "MONITORTIME").and(new Sort(Sort.Direction.DESC, "COMPOSITEVALUE")));

        List<SiteAirQualityPO> resultList = mongoTemplateStandardSiteData.find(query,
                SiteAirQualityPO.class, SITE_AIR_DAY + "-" + smallAirParam.getEndTime().substring(0, 4));

        return resultList;
    }

    @Override
    public List<SiteAirQualityPO> findSiteAirWeekDataBySiteCodesAndTime(SiteAirParam siteAirParam) {
        // 查询条件 增加时间查询条件
        Criteria criteria = Criteria.where("YEAR").is(siteAirParam.getYear()).and("WEEK")
                .is(siteAirParam.getWeek());
        criteria.and("SITECODE").in(siteAirParam.getSiteCodes());

        // 创建查询对象
        Query query = new Query(criteria);

        // 增加排序功能
        query.with(new Sort(Sort.Direction.DESC, "MONITORTIME").and(new Sort(Sort.Direction.DESC, "COMPOSITEVALUE")));
        List<SiteAirQualityPO> resultList = mongoTemplateStandardSiteData.find(query,
                SiteAirQualityPO.class, SITE_AIR_WEEK + "-" + siteAirParam.getYear());
        return resultList;
    }

    @Override
    public List<SiteAirQualityPO> findSiteAirMonthDataBySiteCodesAndTime(SiteAirParam siteAirParam) {
        // 拼凑查询条件
        Criteria criteria = Criteria.where("SITECODE").in(siteAirParam.getSiteCodes());
        criteria.and("MONITORTIME").gte(siteAirParam.getStartTime()).lte(siteAirParam.getEndTime());
        // 创建查询对象,增加排序
        Query query = new Query(criteria).with(new Sort(Sort.Direction.DESC, "MONITORTIME").and(new Sort(Sort.Direction.DESC, "COMPOSITEVALUE")));

        List<SiteAirQualityPO> resultList = mongoTemplateStandardSiteData.find(query,
                SiteAirQualityPO.class, SITE_AIR_MONTH + "-" + siteAirParam.getEndTime().substring(0, 4));
        return resultList;
    }

    @Override
    public List<SiteAirQualityPO> findSiteAirYearDataBySiteCodesAndTime(SiteAirParam siteAirParam) {
        // 拼凑查询条件
        Criteria criteria = Criteria.where("SITECODE").in(siteAirParam.getSiteCodes());
        criteria.and("MONITORTIME").gte(siteAirParam.getStartTime()).lte(siteAirParam.getEndTime());
        // 创建查询对象,增加排序
        Query query = new Query(criteria).with(new Sort(Sort.Direction.DESC, "MONITORTIME").and(new Sort(Sort.Direction.DESC, "COMPOSITEVALUE")));

        List<SiteAirQualityPO> resultList = mongoTemplateStandardSiteData.find(query,
                SiteAirQualityPO.class, SITE_AIR_YEAR + "-" + siteAirParam.getEndTime().substring(0, 4));
        return resultList;
    }

    @Override
    public List<SiteAirQualityPO> findSiteAirHourNewDataBySiteCodes(SiteAirParam siteAirParam) {
        Calendar calendar = Calendar.getInstance();

        // 查询条件 增加时间查询条件
        Criteria criteria = Criteria.where("SITECODE").in(siteAirParam.getSiteCodes());

        // 获取最新记录
        SiteAirQualityPO newData = mongoTemplateStandardSiteData.findOne(
                new Query(criteria).with(new Sort(Sort.Direction.DESC, "MONITORTIME")), SiteAirQualityPO.class,
                SITE_AIR_HOUR +"-" + calendar.get(Calendar.YEAR));
        if (null == newData) {
            return new ArrayList<>();
        }
        // 得到最新时间
        String timeObj = newData.getMONITORTIME();
        // 创建查询对象
        criteria.and("MONITORTIME").is(timeObj);
        Query query = new Query(criteria).with(new Sort(Sort.Direction.DESC, "MONITORTIME"));
        List<SiteAirQualityPO> newDtaList = mongoTemplateStandardSiteData.find(query, SiteAirQualityPO.class,
                SITE_AIR_HOUR +"-" + calendar.get(Calendar.YEAR));
        AQIComparatorMap comparator = new AQIComparatorMap();
//        Collections.sort(newDtaList, comparator);
        return newDtaList;
    }

    @Override
    public List<SiteAirQualityPO> findSiteAirHour12DataBySiteCode(SiteAirParam siteAirParam) {
        // 查询条件 增加时间查询条件
        Criteria criteria = Criteria.where("SITECODE").is(siteAirParam.getSiteCode());
        Calendar calendar = Calendar.getInstance();
        // 获取最新记录
        List<SiteAirQualityPO> newDataList = mongoTemplateStandardSiteData.find(
                new Query(criteria).with(new Sort(Sort.Direction.DESC, "MONITORTIME")).limit(12), SiteAirQualityPO.class,
                SITE_AIR_HOUR +"-"+calendar.get(Calendar.YEAR));

        Collections.reverse(newDataList);
        return newDataList;
    }

    @Override
    public List<SiteAirConcentrationPO> findSiteAirHourConcentrationDataBySiteCodesAndTime(SiteAirParam siteAirParam) {
        // 拼凑查询条件
        Criteria criteria = Criteria.where("SITECODE").in(siteAirParam.getSiteCodes());
        criteria.and("MONITORTIME").gte(siteAirParam.getStartTime()).lte(siteAirParam.getEndTime());
        // 创建查询对象,增加排序
        Query query = new Query(criteria).with(new Sort(Sort.Direction.DESC, "MONITORTIME"));

        List<SiteAirConcentrationPO> resultList = mongoTemplateStandardSiteData.find(query,
                SiteAirConcentrationPO.class, "T_DATA_AIR_HOUR_CONCENTRATION-" + siteAirParam.getEndTime().substring(0, 4));
        return resultList;

    }

}
